package com.stormrage.edu.serv.exam.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.stormrage.edu.dao.exam.ExmStudentExamDao;
import com.stormrage.edu.serv.exam.ExmExamServ;
import com.stormrage.edu.serv.exam.ExmScoreServ;
import com.stormrage.edu.serv.exam.ExmStudentAnswerServ;
import com.stormrage.edu.serv.exam.ExmStudentExamServ;
import com.stormrage.edu.serv.exam.ExmTermScoreServ;
import com.stormrage.edu.serv.learn.LeaStudentServ;
import com.stormrage.edu.serv.learn.LeaTermServ;
import com.stormrage.entity.exam.ExmAnswer;
import com.stormrage.entity.exam.ExmScore;
import com.stormrage.entity.exam.ExmStudentAnswer;
import com.stormrage.entity.exam.ExmStudentExam;
import com.stormrage.entity.exam.ExmTermScore;
import com.stormrage.entity.exam.rep.ExmExamRep;
import com.stormrage.entity.exam.rep.ExmStudentAnswerRep;
import com.stormrage.entity.exam.rep.ExmStudentExamRep;
import com.stormrage.entity.learn.LeaStudent;
import com.stormrage.entity.learn.LeaTerm;
import com.stormrage.entity.learn.req.LeaStudentReq;
import com.stormragetech.core.exception.StormrageTechException;

/**
 * @Title: ExmStudentExamDaoImpl.java
 * @Package com.stormrage.edu.dao.exam.impl
 * @Description: 学员考试试题 Service
 * @author www.stormragetech.com
 * @date 2016年5月17日
 * @version V1.0
 */
@Service
public class ExmStudentExamServImpl implements ExmStudentExamServ {

	private static final Logger logger = LoggerFactory.getLogger(ExmStudentExamServImpl.class);

	@Autowired
	private ExmStudentExamDao exmStudentExamDao;

	@Autowired
	private ExmExamServ exmExamServ;

	@Autowired
	private ExmScoreServ exmScoreServ;

	@Autowired
	private ExmStudentAnswerServ exmStudentAnswerServ;

	@Autowired
	private ExmTermScoreServ exmTermScoreServ;

	@Autowired
	private LeaStudentServ leaStudentServ;

	@Autowired
	private LeaTermServ leaTermServ;

	@Override
	public Integer create(final ExmStudentExam arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExmStudentExamServ 】- 【 create 】-【 STARTED 】");

		Integer index = exmStudentExamDao.create(arg0);

		logger.debug(" 【 Serv 】- 【 ExmStudentExamServ 】- 【 create 】-【 COMPLETED 】");
		return index;

	}

	@Override
	public void delete(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExmStudentExamServ 】- 【 delete 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("stuId", arg0);

		exmStudentExamDao.delete(map);

		logger.debug(" 【 Serv 】- 【 ExmStudentExamServ 】- 【 delete 】-【 COMPLETED 】");

	}

	@Override
	public List<ExmStudentExamRep> queryRepListByStuId(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExmStudentExamServ 】- 【 queryRepListByStuId 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("stuId", arg0);

		List<ExmStudentExamRep> array = exmStudentExamDao.queryRepListByStuId(map);

		logger.debug(" 【 Serv 】- 【 ExmStudentExamServ 】- 【 queryRepListByStuId 】-【 COMPLETED 】");
		return array;

	}

	@Override
	public void createStudentExamForCourse(final LeaStudentReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExmStudentExamServ 】- 【 createStudentExamForCourse 】-【 STARTED 】");

		LeaTerm term = leaTermServ.queryById(arg0.getTermId());
		arg0.setMajorId(term.getMajorId());

		LeaStudent stu = leaStudentServ.queryByAcctMajor(arg0);

		List<ExmExamRep> array = exmExamServ.queryListForCourse(arg0);

		for (ExmExamRep exam : array) {

			ExmStudentExam sexam = ExmStudentExam.instance(stu.getId(), exam.getType(), exam.getQuestion());
			Integer index = create(sexam);

			for (ExmAnswer answer : exam.getList()) {
				ExmStudentAnswer sanswer = ExmStudentAnswer.instance(index, answer.getAnswer(), answer.getCorrect());
				exmStudentAnswerServ.create(sanswer);
			}
		}

		logger.debug(" 【 Serv 】- 【 ExmStudentExamServ 】- 【 createStudentExamForCourse 】-【 COMPLETED 】");

	}

	@Override
	public void createStudentExamForModule(final LeaStudentReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExmStudentExamServ 】- 【 createStudentExamForModule 】-【 STARTED 】");

		LeaTerm term = leaTermServ.queryById(arg0.getTermId());
		arg0.setMajorId(term.getMajorId());

		LeaStudent stu = leaStudentServ.queryByAcctMajor(arg0);

		List<ExmExamRep> array = exmExamServ.queryListForModule(arg0.getTermId());

		for (ExmExamRep exam : array) {

			ExmStudentExam sexam = ExmStudentExam.instance(stu.getId(), exam.getType(), exam.getQuestion());
			Integer index = create(sexam);

			for (ExmAnswer answer : exam.getList()) {
				ExmStudentAnswer sanswer = ExmStudentAnswer.instance(index, answer.getAnswer(), answer.getCorrect());
				exmStudentAnswerServ.create(sanswer);
			}
		}

		logger.debug(" 【 Serv 】- 【 ExmStudentExamServ 】- 【 createStudentExamForModule 】-【 COMPLETED 】");

	}

	@Override
	public List<ExmStudentExamRep> initStudentExamForCourse(final LeaStudentReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExmStudentExamServ 】- 【 initStudentExamForCourse 】-【 STARTED 】");

		LeaTerm term = leaTermServ.queryById(arg0.getTermId());
		arg0.setMajorId(term.getMajorId());

		LeaStudent stu = leaStudentServ.queryByAcctMajor(arg0);

		List<ExmStudentExamRep> array = queryRepListByStuId(stu.getId());

		if (null == array || array.size() < 1) {
			createStudentExamForCourse(arg0);
			array = queryRepListByStuId(stu.getId());
		}

		int seq = 1;

		Iterator<ExmStudentExamRep> it = array.iterator();
		while (it.hasNext()) {
			ExmStudentExamRep entity = it.next();
			List<ExmStudentAnswerRep> list = exmStudentAnswerServ.queryRepByExamId(entity.getId());
			entity = ExmStudentExamRep.instance(entity, seq, list);
			seq++;
		}

		logger.debug(" 【 Serv 】- 【 ExmStudentExamServ 】- 【 initStudentExamForCourse 】-【 COMPLETED 】");
		return array;

	}

	@Override
	public List<ExmStudentExamRep> initStudentExamForModule(final LeaStudentReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExmStudentExamServ 】- 【 initStudentExamForModule 】-【 STARTED 】");

		LeaTerm term = leaTermServ.queryById(arg0.getTermId());
		arg0.setMajorId(term.getMajorId());

		LeaStudent stu = leaStudentServ.queryByAcctMajor(arg0);

		List<ExmStudentExamRep> array = queryRepListByStuId(stu.getId());

		if (null == array || array.size() < 1) {
			createStudentExamForModule(arg0);
			array = queryRepListByStuId(stu.getId());
		}

		int seq = 1;

		Iterator<ExmStudentExamRep> it = array.iterator();
		while (it.hasNext()) {
			ExmStudentExamRep entity = it.next();
			List<ExmStudentAnswerRep> list = exmStudentAnswerServ.queryRepByExamId(entity.getId());
			entity = ExmStudentExamRep.instance(entity, seq, list);
			seq++;
		}

		logger.debug(" 【 Serv 】- 【 ExmStudentExamServ 】- 【 initStudentExamForModule 】-【 COMPLETED 】");
		return array;

	}

	@Override
	public Integer initExamResult(final LeaStudentReq arg0, final HttpServletRequest request)
			throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExmStudentExamServ 】- 【 initExamResult 】-【 STARTED 】");

		LeaTerm term = leaTermServ.queryById(arg0.getTermId());
		arg0.setMajorId(term.getMajorId());

		LeaStudent stu = leaStudentServ.queryByAcctMajor(arg0);

		List<ExmStudentExamRep> array = initStudentExamForCourse(arg0);

		ExmTermScore termScore = exmTermScoreServ.queryByTermId(arg0.getTermId());
		Integer total = 0;
		Integer score = 0;

		for (ExmStudentExamRep entity : array) {
			String result[] = request.getParameterValues(String.valueOf(entity.getId()));
			// logger.debug(" 考试【 " + entity.getSeq() + " 】值为 " + rs);
			if (entity.getType().equals(1)) {
				total += termScore.getSele();
				for (ExmStudentAnswerRep answer : entity.getList()) {
					if (answer.getCorrect().equals(0) && answer.getId().toString().equals(result[0]))
						score += termScore.getSele();
				}
			}
			if (entity.getType().equals(2)) {
				total += termScore.getMult();
				String ans = "";
				String rss = "";
				for (String rs : result)
					rss += rs;
				for (ExmStudentAnswerRep answer : entity.getList()) {
					if (answer.getCorrect().equals(0))
						ans += answer.getId();
				}
				if (ans.equals(rss))
					score += termScore.getMult();

			}
			if (entity.getType().equals(3)) {
				total += termScore.getCorr();
				for (ExmStudentAnswerRep answer : entity.getList()) {
					if (answer.getCorrect().equals(0) && answer.getId().toString().equals(result[0]))
						score += termScore.getCorr();
				}
			}
		}

		score = score * 100 / total;

		ExmScore exmScore = exmScoreServ.queryByStuId(stu.getId());
		if (exmScore.getExam() < score) {
			exmScore.setExam(score);
			exmScoreServ.update(exmScore);
		}

		logger.debug(" 【 Serv 】- 【 ExmStudentExamServ 】- 【 initExamResult 】-【 COMPLETED 】");
		return score;

	}

}
